<template>
  <div class="comment">
    <el-card class="box-card">
      <el-table :data="comments" stripe>
        <!-- <el-table-column type="selection" width="50"> </el-table-column> -->
        <el-table-column type="index" width="50" label=""> </el-table-column>
        <!-- <el-table-column prop="id" label="id"> </el-table-column> -->
        <el-table-column width="80" label="">
          <template slot-scope="sAvatar">
            <el-avatar
              :src="sAvatar.row.avatar"
              :key="sAvatar.row.id"
            ></el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          prop="nickname"
          width="120"
          label="评论人"
        ></el-table-column>
        <el-table-column
          prop="article_name"
          width="120"
          label="职位"
        ></el-table-column>
        <el-table-column
          prop="created_time"
          width="180"
          label="评论时间"
        ></el-table-column>
        <el-table-column
          prop="content"
          width="180"
          label="评论内容"
        ></el-table-column>
        <el-table-column align="right" fixed="right">
          <template slot="header"> 过审 </template>
          <template slot-scope="sOperate">
            <el-button
              :type="sOperate.row.approved ? 'success' : 'info'"
              :icon="sOperate.row.approved ? 'el-icon-check' : 'el-icon-close'"
              circle
              @click="handleEdit(sOperate.$index, sOperate.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column align="right" fixed="right">
          <template slot="header"> 删除 </template>
          <template slot-scope="sOperate">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="handleDelete(sOperate.$index, sOperate.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[3, 5, 10, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>

      <div class="batch-del">
        <!-- <el-button type="danger">批量删除</el-button> -->
        <!-- <el-input v-model="search" placeholder="输入内容搜索"></el-input> -->
      </div>
    </el-card>
    <!-- <el-dialog
      :title="'111'"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-input placeholder="请输入内容" v-model="comment.comment_name">
        <template slot="prepend">分类名</template>
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cacel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
export default {
  name: 'Comment',
  created () {
    this.getData()
  },
  data () {
    return {
      total: 0,
      admin: [],
      comments: [],
      search: '22',
      page: 1,
      size: 5,
      isEdit: false,
      centerDialogVisible: false,
      id: '',
      isAdmin: false,
      comment: {
        comment_name: ''
      }
    }
  },
  methods: {
    // 分页获取评论
    getData () {
      this.$http
        .get(`/comments/?page=${this.page}&size=${this.size}`)
        .then(res => {
          const { code, result } = res
          if (code == 0) {
            this.total = result.total
            this.comments = result.comments
          }
        })
    },
    // 修改评论
    handleEdit (index, row) {
      this.$http
        .put(`/comments/${row.id}`, { approved: !!row.approved ? 0 : 1 })
        .then(res => {
          const { code } = res
          if (code == 0) {
            this.getData()
          } else {
            this.$message({
              type: 'info',
              message: '修改失败'
            })
          }
        })
    },
    // 删除评论
    handleDelete (index, row) {
      this.$confirm('确定删除此评论吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.delete(`/comments?id=${row.id}`).then(res => {
            const { code } = res
            if (code == 0) {
              this.getData()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message({
                type: 'info',
                message: '删除失败'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleSizeChange (val) {
      this.size = val
      this.getData()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getData()
    },
    // 对话框确认修改
    save () {
      this.$http.put(`/comments/?id=${this.id}`, this.comment).then(res => {
        this.id = ''
        this.comment.comment_name = ''
        if (res.code == 0) {
          this.getData()
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: '修改失败'
          })
        }
      })
      this.centerDialogVisible = false
      this.isEdit = false
    },
    // 对话框取消
    cacel () {
      this.centerDialogVisible = false
    },
    searchcomment () {
      // console.log(this.search)
    }
  }
}
</script>

<style>
.comment .el-card {
  display: flex;
  justify-content: center;
  width: 100%;
  margin-bottom: 25px;
}
.batch-del {
  margin-top: 10px;
}
.el-dialog .el-input {
  margin-top: 10px;
}
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
